<template>
    <el-card>
        <div slot="header" class="clearfix">
            <h3>修改学生</h3>
        </div>
        <!-- 新增内容框 -->
        <el-form :model="updateStudent" label-width="100px">
            <el-form-item label="学生姓名：">
                <el-input v-model="updateStudent.name"></el-input>
            </el-form-item>
            <el-form-item label="学生年龄：">
                <el-input v-model="updateStudent.age"></el-input>
            </el-form-item>
            <el-form-item label="学生性别：">
                <el-radio-group v-model="updateStudent.gender">
                    <el-radio label="男"></el-radio>
                    <el-radio label="女"></el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="学生专业：">
                <el-select v-model="updateStudent.subjectsId" placeholder="请选择专业" @change="clearClassId">
                    <el-option v-for="item in subjectsData" :key="item._id" :label="item.name" :value="item._id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="学生班级：">
                <el-select v-model="updateStudent.classesId" placeholder="请选择班级">
                    <el-option v-for="item in classesData" :key="item._id" :label="item.name" :value="item._id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="上传图片：">
                <el-upload class="avatar-uploader" action="http://47.98.128.191:3001/images/uploadImages"
                    :show-file-list="false" :on-success="handleAvatarSuccess">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="updateStudentSubmit(updateStudent)">确认修改</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
    import {
        createNamespacedHelpers
    } from "vuex";
    // 引入学生仓库
    const {
        mapActions: studentsMapActions,
    } = createNamespacedHelpers(
        "students"
    );
    // 引入班级仓库
    const {
        mapActions: classesMapActions,
    } = createNamespacedHelpers(
        "classes"
    );
    // 引入专业仓库
    const {
        mapState: subjectsMapState,
        mapActions: subjectsMapActions,
    } = createNamespacedHelpers(
        "subjects"
    );
    export default {
        data() {
            return {
                _id: '',
                updateStudent: {
                    name: '',
                    age: '',
                    gender: '',
                    subjectsId: '',
                    classesId: '',
                    image: '',
                },
                classesData: [],
                imageUrl: '',
            };
        },
        computed: {
            ...subjectsMapState(['subjectsData']),
        },
        mounted() {
            this._id = this.$route.params._id;
            this.getOneStudentInfo(this._id);
            this.getSubjects();
        },
        watch: {
            'updateStudent.subjectsId'(newval, oldval) {
                this.getClassData(newval);
            },
            'updateStudent.image'(val) {
                this.imageUrl = val;
            }
        },
        methods: {
            ...studentsMapActions(['getOneStudentById', 'UpdateOneStudent']),
            ...subjectsMapActions(['getSubjects']),
            ...classesMapActions(['getClassesBySubjectsId']),
            // 获取修改学生全部信息
            async getOneStudentInfo(_id) {
                const res = await this.getOneStudentById(_id);
                if (res.code) {
                    this.updateStudent = res.data;
                }
            },
            // 根据获取的学生专业查找班级
            async getClassData(subjectsId) {
                const res = await this.getClassesBySubjectsId(subjectsId);
                if (res.code) {
                    this.classesData = res.data.result;
                }
            },
            // 上传头像图片
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                this.updateStudent.image = "http://47.98.128.191:3001" + res.data[0];
            },
            async updateStudentSubmit(updateStudent) {
                const res = await this.UpdateOneStudent(updateStudent);
                if (res.code) {
                    this.$message({
                        message: '修改成功！',
                        type: 'success',
                        center: true,
                    });
                    this.$router.push('/home/studentsList');
                } else {
                    this.$message({
                        message: '修改失败！',
                        type: 'error',
                        center: true,
                    });
                }
            },
            //改变subjectId后将classesId清空，重新获取
            clearClassId() {
                this.updateStudent.classesId = '';
            }
        }
    }
</script>

<style lang="scss" scoped>
    .el-card {

        .clearfix:before,
        .clearfix:after {
            display: table;
            content: "";
        }

        .clearfix:after {
            clear: both
        }

        .el-form {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;

            .el-form-item {
                width: 400px;

                .el-button {
                    margin-top: 30px
                }

                ;

                .avatar-uploader {
                    width: 100px;
                    border: 1px dashed #666;
                    border-radius: 6px;
                    cursor: pointer;
                    position: relative;
                    overflow: hidden;
                }

                .avatar-uploader:hover {
                    border-color: #409EFF;
                }

                .avatar-uploader-icon {
                    font-size: 28px;
                    color: #8c939d;
                    width: 100px;
                    height: 100px;
                    line-height: 100px;
                    text-align: center;
                }

                .avatar {
                    width: 100px;
                    height: 100px;
                    display: block;
                }
            }
        }
    }
</style>